package cn.weibo.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;

import cn.weibo.entity.UserEntity;
import cn.weibo.env.constants.ApplicationConstants;

@Controller
public class ApplicationController {

	private final Logger log = LoggerFactory.getLogger(ApplicationController.class);

    @RequestMapping("/")
    public String home(HttpServletRequest request, Model model) {

        HttpSession session = request.getSession();
        UserEntity user = (UserEntity) session.getAttribute(ApplicationConstants.APPLICATION_USER_ATTRIBUTE);
        if (user != null) {
            model.addAttribute("logined", true);
        } else {

            model.addAttribute("logined", false);
        }
        return "/views/index";
    }
	
	
	@RequestMapping("/notFound")
	@ResponseStatus(value = HttpStatus.NOT_FOUND)
	public String notFound(HttpServletRequest request) {
		request.setAttribute("message", "404 Not Found");
		DateTime dateTime = new DateTime();
		String time = dateTime.toString("HH:mm:ss");
		String date = dateTime.toString("yyyy-MM-dd");
		request.setAttribute("time", time);
		request.setAttribute("date", date);

		return "/views/error/message";
	}
}
